package com.bangcommunity.bbframe.http.interceptor;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author tanghc
 * @date 18/1/17
 */
public class LoggingInterceptor implements Interceptor {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request request = chain.request();

        long t1 = System.nanoTime();
        HttpUrl url = request.url();
        logger.info("Sending request {}", url);
        Response response = chain.proceed(request);

        long t2 = System.nanoTime();
        logger.info("Received response for {}, {}, {}ms", response.request().url(), response.code(), (t2 - t1) / 1e6d);

        return response;
    }
}
